#define _CRT_SECURE_NO_WARNINGS 1

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    bool findTarget(TreeNode* root, int k) {
        if (root == nullptr || root->left == nullptr && root->right == nullptr)
            return false;

        vector<int> v1;
        GetVal(root, v1);
        sort(v1.begin(), v1.end());
        // for(int i = v1.size() - 1; i > 0; --i)
        // {
        //     if(v1[i] + v1[0] <= k)
        //     {
        //         v1.resize(i);
        //         break;
        //     }
        // }
        for (int i = v1.size() - 1; i > 0; --i)
        {
            for (int j = 0; j < i; ++j)
            {
                if (v1[i] + v1[j] == k)
                {
                    return true;
                }
            }
        }

        return false;
    }
    void GetVal(TreeNode* node, vector<int>& v1)
    {
        if (node == nullptr)
            return;
        v1.push_back(node->val);
        GetVal(node->left, v1);
        GetVal(node->right, v1);
    }
};